import java.net.*;
import java.nio.channels.*;
import java.io.*;

class ServiceTask implements Runnable
{
   SocketChannel channel;

   public ServiceTask(SocketChannel readyChannel)
   {
      channel = readyChannel;
   }

   public void run()
   {

      InputStream input;
      int available = 0;
      try
      {
         input = channel.socket().getInputStream();
         available = input.available();
      }
      catch (IOException e)
      {
         try{
            channel.close();
         }
         catch(IOException ioEx)
         {
         }
         return;

      }


      if (available < 1)
      {
         System.out.println("Something broke.  This should have been available.");
         return;
      }


      try{
         Thread.sleep(1000);
      }
      catch(InterruptedException intEx){}

      try
      {
         byte[] buf = new byte[available];

         input.read(buf, 0, available);
         System.out.println(new String(buf));
      }
      catch(IOException e)
      {
         System.out.println("Something broke.  This should have been available.");
         return;

      }


      System.out.println("Ready to close");
      try{
         channel.close();
      }
      catch(IOException ioEx){}

   }


}
